package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 话题查询：按照发布人或发布时间
 *
 * @author wu
 */
public class CompanyforumTopicQueryFragment extends Fragment implements OnClickListener {

  public static final String EXTRA_STAFF_ID = "com.isunland.managesystem.ui.EXTRA_STAFF_ID";
  public static final String EXTRA_BEGIN_DATE = "com.isunland.managesystem.ui.EXTRA_BEGIN_DATE";
  public static final String EXTRA_END_DATE = "com.isunland.managesystem.ui.EXTRA_END_DATE";
  public static final String EXTRA_STAFF_NAME = "com.isunland.managesystem.ui.EXTRA_STAFF_NAME";
  public static final String REQUEST_STAFF_NAME = "com.isunland.managesystem.ui.REQUEST_STAFF_NAME";
  public static final String REQUEST_START_DATE = "com.isunland.managesystem.ui.REQUEST_START_DATE";
  public static final String REQUEST_END_DATE = "com.isunland.managesystem.ui.REQUEST_END_DATE";
  private static final int REQUEST_STAFF = 0;
  private static final int REQUEST_ACQUIRE_STARTTIME = 1;
  private static final int REQUEST_ACQUIRE_FINISHTIME = 2;
  private TextView mtvTopicPublisherTopicQuery;// 发布人
  private TextView mstartTime;// 发布时间
  private TextView mEndTime;//
  //private String mMemberCode;
  private String mStaffName;
  private String mjobNos;
  private String mBeginDate;
  private String mEndDate;
  private CurrentUser mCurrentUser;

  public static Fragment newInstance(String staffName, String start, String end) {
    Bundle bundle = new Bundle();
    bundle.putString(REQUEST_STAFF_NAME, staffName);
    bundle.putString(REQUEST_START_DATE, start);
    bundle.putString(REQUEST_END_DATE, end);
    CompanyforumTopicQueryFragment fragment = new CompanyforumTopicQueryFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    BaseVolleyActivity mActivity = (BaseVolleyActivity) getActivity();

    if (NavUtils.getParentActivityName(getActivity()) != null) {
      if (mActivity.getSupportActionBar() != null) {
        mActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      }
    }
    if (mActivity.getSupportActionBar() != null) {
      mActivity.getSupportActionBar().setTitle(R.string.topicQuery);
    }

    mCurrentUser = CurrentUser.newInstance(getActivity());
    mjobNos = mCurrentUser.getJobNumber();
    mStaffName = getArguments().getString(REQUEST_STAFF_NAME);
    mBeginDate = getArguments().getString(REQUEST_START_DATE);
    mEndDate = getArguments().getString(REQUEST_END_DATE);
    if (TextUtils.isEmpty(mStaffName)) {
      mStaffName = mCurrentUser.getRealName();
    }

    if (TextUtils.isEmpty(mBeginDate)) {
      mBeginDate = MyDateUtil.getBeforeMonth();
    }
    if (TextUtils.isEmpty(mEndDate)) {
      mEndDate = MyDateUtil.parseDateToString(new Date());
    }

    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_topic_query, container, false);
    mtvTopicPublisherTopicQuery = (TextView) view.findViewById(R.id.tv_topicPublisher_topicQuery);
    mstartTime = (TextView) view.findViewById(R.id.tv_startTime__topicQuery);
    mEndTime = (TextView) view.findViewById(R.id.tv_endTime__topicQuery);
    mtvTopicPublisherTopicQuery.setText(mStaffName);
    mstartTime.setText(mBeginDate);
    mEndTime.setText(mEndDate);
    mtvTopicPublisherTopicQuery.setOnClickListener(this);
    mstartTime.setOnClickListener(this);
    mEndTime.setOnClickListener(this);
    return view;
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      // 发布人
      case R.id.tv_topicPublisher_topicQuery:
        Intent intent = new Intent(getActivity(), AppointerListActivity.class);
        startActivityForResult(intent, REQUEST_STAFF);
        break;

      // 发布时间
      case R.id.tv_startTime__topicQuery:
        showDialog(REQUEST_ACQUIRE_STARTTIME, new Date());
        break;
      // 发布时间
      case R.id.tv_endTime__topicQuery:
        showDialog(REQUEST_ACQUIRE_FINISHTIME, new Date());
        break;

      default:
        break;
    }
  }

  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog;

    switch (requestCode) {

      case REQUEST_ACQUIRE_STARTTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, R.string.choseBeginDate);
        break;
      case REQUEST_ACQUIRE_FINISHTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, R.string.choseEndDate);
        break;
      default:
        return;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(CompanyforumTopicQueryFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
      /**
       * 响应返回按钮
       */
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:
        String start = mstartTime.getText().toString().trim();
        String end = mEndTime.getText().toString().trim();
        String staffName = mtvTopicPublisherTopicQuery.getText().toString().trim();
        intent = new Intent();
        intent.putExtra(EXTRA_STAFF_ID, mjobNos);
        intent.putExtra(EXTRA_STAFF_NAME, staffName);
        intent.putExtra(EXTRA_BEGIN_DATE, start);
        intent.putExtra(EXTRA_END_DATE, end);
        intent.putExtra(CompanyForumListFragment.EXTRA_QUERY_TYPE,
            CompanyForumListFragment.EXTRA_TOPIC_QUERY);
        //			startActivity(intent);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_STAFF && data != null) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mStaffName = customer.getName();
      mjobNos = customer.getId();
      LogUtil.i("mjobNos===---===---" + mjobNos);
      //LogUtil.i("mMemberCode===---===---" + mMemberCode);
      mtvTopicPublisherTopicQuery.setText(mStaffName);
    }

    if (requestCode == REQUEST_ACQUIRE_STARTTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mBeginDate = DateUtil.dateToString(acquireDate, DateUtil.TYPE_DATE);
      getActivity().setResult(REQUEST_ACQUIRE_FINISHTIME);
      mstartTime.setText(mBeginDate);
      return;
    }

    if (requestCode == REQUEST_ACQUIRE_FINISHTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mEndDate = DateUtil.dateToString(acquireDate, DateUtil.TYPE_DATE);
      mEndTime.setText(mEndDate);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
